###Script to Code variables---------------------------

###Clear R------------------------
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}


#function to install packages if they don't exist
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("psych", "ggplot2", "interplot", "MASS", "foreign",  "car", "stringr", "stargazer", "xtable", "moments", "lavaan")
ipak(packages)

#Load data
load("Study4_replication.Rdata")

### Partisanship-----------
data$partisanship <- NA
data$partisanship[which(data$pid3==1)]=1
data$partisanship[which(data$pid3==2)]=2
data$partisanship[which(data$pid4==2)]=3
data$partisanship[which(data$pid4==3)]=4
data$partisanship[which(data$pid4==1)]=5
data$partisanship[which(data$pid2==2)]=6
data$partisanship[which(data$pid2==1)]=7

#PID strength
data$pidstrength <- (zero1(abs(data$partisanship-4)))


### Partisan SoCial Identity Strength------------
data$strength_dem_1_rec<-car::recode(data$strength_dem_1, "36=1;37=2; 39=3;40=4;else=NA")
data$strength_dem_2_rec<-car::recode(data$strength_dem_2, "36=1;37=2; 39=3;40=4;else=NA")
data$strength_dem_3_rec<-car::recode(data$strength_dem_3, "36=1;37=2; 39=3;40=4;else=NA")
data$strength_dem_4_rec<-car::recode(data$strength_dem_4, "36=1;37=2; 39=3;40=4;else=NA")
data$strength_dem_5_rec<-car::recode(data$strength_dem_5, "36=1;37=2; 39=3;40=4;else=NA")
data$strength_dem_6_rec<-car::recode(data$strength_dem_6, "36=1;37=2; 39=3;40=4;else=NA")
data$strength_dem_7_rec<-car::recode(data$strength_dem_7, "36=1;37=2; 39=3;40=4;else=NA")
data$strength_dem_8_rec<-car::recode(data$strength_dem_8, "36=1;37=2; 39=3;40=4;else=NA")

data$strength_rep_1_rec<-car::recode(data$strength_rep_1, "10=1;11=2; 13=3;14=4;else=NA")
data$strength_rep_2_rec<-car::recode(data$strength_rep_2, "10=1;11=2; 13=3;14=4;else=NA")
data$strength_rep_3_rec<-car::recode(data$strength_rep_3, "10=1;11=2; 13=3;14=4;else=NA")
data$strength_rep_4_rec<-car::recode(data$strength_rep_4, "10=1;11=2; 13=3;14=4;else=NA")
data$strength_rep_5_rec<-car::recode(data$strength_rep_5, "10=1;11=2; 13=3;14=4;else=NA")
data$strength_rep_6_rec<-car::recode(data$strength_rep_6, "10=1;11=2; 13=3;14=4;else=NA")
data$strength_rep_7_rec<-car::recode(data$strength_rep_7, "10=1;11=2; 13=3;14=4;else=NA")
data$strength_rep_8_rec<-car::recode(data$strength_rep_8, "10=1;11=2; 13=3;14=4;else=NA")

data$huddy1 <- ifelse(!is.na(data$strength_dem_1_rec),data$strength_dem_1_rec,data$strength_rep_1_rec)
data$huddy2 <- ifelse(!is.na(data$strength_dem_2_rec),data$strength_dem_2_rec,data$strength_rep_2_rec)
data$huddy3 <- ifelse(!is.na(data$strength_dem_3_rec),data$strength_dem_3_rec,data$strength_rep_3_rec)
data$huddy4 <- ifelse(!is.na(data$strength_dem_4_rec),data$strength_dem_4_rec,data$strength_rep_4_rec)
data$huddy5 <- ifelse(!is.na(data$strength_dem_5_rec),data$strength_dem_5_rec,data$strength_rep_5_rec)
data$huddy6 <- ifelse(!is.na(data$strength_dem_6_rec),data$strength_dem_6_rec,data$strength_rep_6_rec)
data$huddy7 <- ifelse(!is.na(data$strength_dem_7_rec),data$strength_dem_7_rec,data$strength_rep_7_rec)
data$huddy8 <- ifelse(!is.na(data$strength_dem_8_rec),data$strength_dem_8_rec,data$strength_rep_8_rec)

data$PSIDstrength<-1-rowMeans(mapply(function(x)zero1(as.numeric(x)),with(data,data.frame(huddy1, huddy2, huddy3, huddy4, huddy5, huddy6, huddy7, huddy8))),na.rm=T) #note that here scale gets recoded to range from weak (0) to strong (1) pid strength

### Cognitive Reflection Test---------------------

#CRT
data$CRT1 <- tolower(data$CRT1)
data$CRT1[str_detect(data$CRT1,"eight")]="8"
data$CRT1[str_detect(data$CRT1,"8")]="8"
unique(data$CRT1)
data$CRT1 <- ifelse(data$CRT1==8,1,0)

data$CRT2 <- tolower(data$CRT2)
data$CRT2[str_detect(data$CRT2,"was the person in 2nd place")]="0"
data$CRT2[str_detect(data$CRT2,"second")]="2"
data$CRT2[str_detect(data$CRT2,"2nd")]="2"
data$CRT2[str_detect(data$CRT2,"2")]="2"
data$CRT2[str_detect(data$CRT2,"scond")]="2"
data$CRT2[str_detect(data$CRT2,"two")]="2"
data$CRT2 <- ifelse(data$CRT2==2,1,0)

data$CRT3 <- tolower(data$CRT3)
data$CRT3[str_detect(data$CRT3,"emily")]="emily"
data$CRT3[str_detect(data$CRT3,"emiy")]="emily"
data$CRT3[str_detect(data$CRT3,"emely")]="emily"
data$CRT3[str_detect(data$CRT3,"emilly")]="emily"
data$CRT3 <- ifelse(data$CRT3=="emily",1,0)

data$CRT4 <- tolower(data$CRT4)
data$CRT4[str_detect(data$CRT4,"no dirt, or 27")]="1"
data$CRT4[str_detect(data$CRT4,"0.02")]="1"
data$CRT4[str_detect(data$CRT4,"0.015625")]="1"
data$CRT4[str_detect(data$CRT4,".02")]="1"
data$CRT4[str_detect(data$CRT4,"0")]="none"
data$CRT4[str_detect(data$CRT4,"none")]="none"
data$CRT4[str_detect(data$CRT4,"none.")]="none"
data$CRT4[str_detect(data$CRT4,"zero")]="none"
data$CRT4[str_detect(data$CRT4,"no dirt in a hole")]="none"
data$CRT4[str_detect(data$CRT4,"no dirt")]="none"
data$CRT4[str_detect(data$CRT4,"nonw")]="none"
data$CRT4[str_detect(data$CRT4,"none-it's")]="none"
data$CRT4 <- ifelse(data$CRT4=="none",1,0)

data$CRT5 <- tolower(data$CRT5)
data$CRT5[str_detect(data$CRT5,"4 days")]=4
data$CRT5[str_detect(data$CRT5,"4 d")]=4
data$CRT5[str_detect(data$CRT5,"four days")]=4
data$CRT5[str_detect(data$CRT5,"four")]=4
data$CRT5 <- ifelse(data$CRT5==4,1,0)

data$CRT6 <- tolower(data$CRT6)
data$CRT6[str_detect(data$CRT6,"29")]=29
data$CRT6[str_detect(data$CRT6,"29 students")]=29
data$CRT6[str_detect(data$CRT6,"29 people")]=29
data$CRT6 <- ifelse(data$CRT6==29,1,0)

data$CRT7 <- tolower(data$CRT7)
data$CRT7[str_detect(data$CRT7,"20")]=20
data$CRT7[str_detect(data$CRT7,"2o")]=20
data$CRT7[str_detect(data$CRT7,"2o")]=20
data$CRT7 <- ifelse(data$CRT7==20,1,0)

data$CRTall <- rowSums(mapply(FUN = as.numeric,with(data,data.frame(CRT1,CRT2,CRT3,CRT4,CRT5,CRT6,CRT7))),na.rm=T)
data$CRTall<-zero1(data$CRTall)

#CRT 3 from the CRT7
data$CRT7_3<-rowSums(mapply(FUN = as.numeric,with(data,data.frame(CRT5,CRT6,CRT7))),na.rm=T)
data$CRT7_3<-zero1(data$CRT7_3)
#CRT-2
data$CRT2<-rowSums(mapply(FUN = as.numeric,with(data,data.frame(CRT1,CRT2,CRT3, CRT4))),na.rm=T)
data$CRT2<-zero1(data$CRT2)


### Party Cues----------------

#In-party
# In-party endorsement: 0 (out-party endorses ban or no party cues); 1 (leaning, weak or strong partisan, in-party endorses ban). 
#Inparty endorsement
data$InParty<-NA
data$InParty[(data$treatment==3 & data$pid1==2) | (data$treatment==3 & data$pid4==2) | (data$treatment==2 & data$pid1==1 )| (data$treatment==2 & data$pid4==1)|data$treatment==1]=0
data$InParty[(data$treatment==2 & data$pid1==2 )| (data$treatment==2 & data$pid4==2 )]=1
data$InParty[(data$treatment==3 & data$pid1==1 )| (data$treatment==3 & data$pid4==1) ]=1

#Out-party
#Out-party endorsement: 0 (in-party endorses ban or no party cues); 1 (leaning partisan; weak or strong partisan, out-party endorses ban).
#outparty
data$OutParty<-NA
data$OutParty[data$treatment==3 & data$pid1==1 | data$treatment==3 & data$pid4==1 | data$treatment==2 & data$pid1==2 | data$treatment==2 & data$pid4==2 | data$treatment==1]=0
data$OutParty[data$treatment==2 & data$pid1==1 | data$treatment==2 & data$pid4==1 ]=1
data$OutParty[data$treatment==3 & data$pid1==2 | data$treatment==3 & data$pid4==2 ]=1

#party cue vs. no cue
data$partycue<-ifelse(data$treatment==1,0,1)

### Dependent Variable --------------
data$DV_support_rec<-(6-data$DV_support)
#Item 2: Costs outweigh benefits
#recode so that opposition is the highest value
data$DV_costs_rec<-(6-data$DV_costs)
#Item 3: food irradiation is bad
#qualtrics coded item wrong
data$DV_bad<-data$DV_bad-5
#recode so that opposition is the highest value
data$DV_bad_rec<-(6-data$DV_bad)

#create mean
data$DV_irradiation<-rowMeans(data.frame(data$DV_support_rec, data$DV_costs_rec,data$DV_bad_rec), na.rm=T)
data$DV_irradiation<-zero1(data$DV_irradiation)

### Correct answers on the quiz--------------
data$Quiz1_cor<-ifelse(data$Quiz1==3,1,0)
data$Quiz2_cor<-ifelse(data$Quiz2==2,1,0)
data$Quiz3_cor<-ifelse(data$Quiz3==1,1,0)
data$Quiz4_cor<-ifelse(data$Quiz4==3,1,0)
data$Quiz_correct<-(data$Quiz1_cor+data$Quiz2_cor+data$Quiz3_cor+data$Quiz4_cor)

### Formulated thoughts--------
#number of characters
j<-as.vector(data$Thoughts)
data$nchar1 <- c(nchar(j))
summary(data$nchar1) #summary of #n of characters
#number of words
data$words<-sapply(strsplit(j, " "), length)

### Covariates -----------------

#Age - many younger folks
table(data$Age)

#sex - lot more woman (2) compared to men (1)
data$female<-ifelse(data$Gender==2,1,0)

#education - high educated
data$Education<-car::recode(data$Education, "2=1; 3=2; 4=3; 5=3")

#race - mostly white
data$non_white<-ifelse(data$Race==1,0,1)

#party
data$Republican_dummy<-0
data$Republican_dummy[data$pid1==1]=1
data$Republican_dummy[data$pid4==1]=1

#save data
save(data, file="Study4_data.Rdata")
